var plugin_adminui_autosave_lastpostdata = '';

function plugin_adminui_autosave_message(str, cssclass)
{
   autosaveMessage = $('#admin-plus-autosave-message');
   if(autosaveMessage.length <= 0) {
      autosaveMessage = $("<span id='admin-plus-autosave-message' class='"+cssclass+"'></span>");
   }

   autosaveMessage.text(str);

   $('body').prepend(autosaveMessage);
}

function plugin_adminui_autosave_timenow(){
    var now= new Date(),
    ampm= 'am',
    h= now.getHours(),
    m= now.getMinutes(),
    s= now.getSeconds();
    if(h>= 12){
        if(h>12)h-= 12;
        ampm= 'pm';
    }
    if(h<10) h= '0'+h;
    if(m<10) m= '0'+m;
    if(s<10) s= '0'+s;
    return now.toLocaleDateString()+' '+h+':'+m+':'+s+' '+ampm;
}
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){

	// get current url, and check if id key param is on the url
	var pathname = $(location).attr('href');
//	searchtext = "?id=";
//	containsIdText = pathname.toLowerCase().indexOf(searchtext.toLowerCase());
   containsIdText = parseInt($('input[name="id"]').val());
   
   
   var elstatus = $('select[name="status"]');
   var valstatus = parseInt(parseInt(elstatus.val()));
   
	// if true, proceed to access the id
	if (containsIdText > 0 && valstatus != 1) {
		// set a 10s interval to post form
		setInterval(function() {
         var form = $('#content-admin-post');
         var richtext = CKEDITOR.instances['content'].getData();
         $('textarea[name="content"]').val(richtext);
         var serialized = form.serialize();
         
         
         if(serialized.localeCompare(plugin_adminui_autosave_lastpostdata) == 0) {
            plugin_adminui_autosave_message('No changes', 'template-info');
         } else {
            plugin_adminui_autosave_lastpostdata = serialized;
            
            $.ajax({
               type: "POST",
               url: pathname,
               data: serialized,
               success:function() {
                  plugin_adminui_autosave_message('Last saved: ' + plugin_adminui_autosave_timenow(), 'ui-state-highlight');
               },

               failure: function() {
                  alert('Error auto saving!!');
               }
            });
         }
         
		}, plugin_adminplus_autosave_interval);
	} else {
      var error = '';
      if(valstatus == 1) {
         error += 'LIVE content.';
      } 
      
      if(!containsIdText) {
         error += ' Content ID missing.';
      }
      
      plugin_adminui_autosave_message('Auto saved is inactive. (' + error  + ')', 'ui-state-error');
   }
	
});


// extension to retrieve params from url (by getUrlVars for all objects) or (by getUrlVar('item') for one object)
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});